<?php
/*************************************************************************************
 *
 *	FileName: Mailer.php
 *	Desc    : Put all mail functions in the class
 *	Author  : Andrew
 *	Date    : 2010-02-25
 *
 *************************************************************************************/

class Mailer
{
	private $mail;    
	private $content_pefix;
	private $content_suffix;
	
	//initilize the mail class
	public function __construct()
    {
    	$this->content_pefix = '<html><head><meta http-equiv=Content-Type content="text/html; charset='.MAIL_CHARSET.'"></head><body>';
    	$this->content_suffix = '</body></html>';
    	
    	$this->mail=GetPhpMailer();
    	$this->mail->IsSMTP();
    	$this->mail->Host = MAIL_HOST;
    	//$this->mail->SMTPAuth = true;
    	$this->mail->Username = MAIL_USERNAME;
    	$this->mail->Password = MAIL_PASSWORD;       
		$this->mail->From = MAIL_FROM;      
		$this->mail->FromName = MAIL_FROMNAME;
		$this->mail->IsHTML(true);
		$this->mail->CharSet = MAIL_CHARSET;
		$this->mail->Encoding = MAIL_ENCODING; 
		$this->mail->AddReplyTo( MAIL_FROM, MAIL_FROMNAME);
    }
    
    private function CreateContent($template)
    {
    	return $this->content_pefix.$this->mail->getFile($template).$this->content_suffix;
    }
    
    public function SendRegisterMail($email, $nick, $activationLink)
    {
    	$this->mail->Subject = $nick.'，欢迎成为花开里会员!';
    	$this->mail->Subject = mb_convert_encoding($this->mail->Subject ,MAIL_CHARSET,"auto");
    	
    	$content = $this->CreateContent(REGISTER_MAIL);
    	$content = str_replace('%nick%', $nick, $content);
    	$content = str_replace('%time%', GetCurrentDate('Y-n-j'), $content);
        $content = str_replace('%url%', $activationLink, $content);
    	$this->mail->Body = mb_convert_encoding($content ,MAIL_CHARSET,"auto");	
    	$this->mail->ResetAddress();
    	$this->mail->AddAddress($email, $nick);  
  		
    	$this->mail->Send();
    }
    
    public function SendRegisterWithInviterMail($email, $nick, $inviterName, $inviterID, $activationLink)
    {
    	$this->mail->Subject = $nick.'，欢迎成为花开里会员!';
    	$this->mail->Subject = mb_convert_encoding($this->mail->Subject ,MAIL_CHARSET,"auto");
    	
    	$content = $this->CreateContent(REGISTER_WITH_INVITER_MAIL);
    	$content = str_replace('%nick%', $nick, $content);
    //	$content = str_replace('%inviterName%', $inviterName, $content);
    //	$content = str_replace('%inviterID%', $inviterID, $content);
    	$content = str_replace('%time%', GetCurrentDate('Y-n-j'), $content);
        $content = str_replace('%url%', $activationLink, $content);
    	$this->mail->Body = mb_convert_encoding($content ,MAIL_CHARSET,"auto");	
    	$this->mail->ResetAddress();
    	$this->mail->AddAddress($email, $nick);  
    	
    	$this->mail->Send();
    }
    
    public function SendInviteSuccessMail($email, $nick, $invitedName, $invitedID)
    {
    	$this->mail->Subject = '好友邀请成功通知';
    	$this->mail->Subject = mb_convert_encoding($this->mail->Subject ,MAIL_CHARSET,"auto");
    	
    	$content = $this->CreateContent(INVITE_SUCCESS_MAIL);
    	$content = str_replace('%nick%', $nick, $content);
    	$content = str_replace('%invitedName%', $invitedName, $content);
    	$content = str_replace('%invitedID%', $invitedID, $content);
    	$content = str_replace('%time%', GetCurrentDate('Y-n-j'), $content);
    	$this->mail->Body = mb_convert_encoding($content ,MAIL_CHARSET,"auto");	
    	$this->mail->ResetAddress();
    	$this->mail->AddAddress($email, $nick);  
  		
    	$this->mail->Send(); 
    }
    
    public function SendFindPwdMail($email, $nick, $pwd)
    {
    	$this->mail->Subject = '花开里密码找回';
    	$this->mail->Subject = mb_convert_encoding($this->mail->Subject ,MAIL_CHARSET,"auto");
    	
    	$content = $this->CreateContent(FIND_PWD_MAIL);
    	$content = str_replace('%nick%', $nick, $content);
    	$content = str_replace('%pwd%', $pwd, $content);
    	$content = str_replace('%time%', GetCurrentDate(), $content);
    	$this->mail->Body = mb_convert_encoding($content ,MAIL_CHARSET,"auto");	
    	$this->mail->ResetAddress();
    	$this->mail->AddAddress($email, $nick);  
  		
    	$this->mail->Send(); 
    }
    
    public function SendInviteMail($invitedEmail, $inviteEmail, $inviteID)
    {
    	$this->mail->Subject = '您的好友邀您加入花开里，并成为他好友';
    	$this->mail->Subject = mb_convert_encoding($this->mail->Subject ,MAIL_CHARSET,"auto");
    	
    	$content = $this->CreateContent(INVITE_MAIL);
    	$content = str_replace('%inviteEmail%', $inviteEmail, $content);
    	$content = str_replace('%inviteID%', $inviteID, $content);
    	$content = str_replace('%time%', GetCurrentDate('Y-n-j'), $content);
    	$this->mail->Body = mb_convert_encoding($content ,MAIL_CHARSET,"auto");	
    	$this->mail->ResetAddress();
    	
    	foreach($invitedEmail as $k=>$v){
    		
    	    	$this->mail->AddAddress($v);  	
    	}

    	$this->mail->Send(); 
    }
}

?>